package fr.afcepf.ai85.gepeto.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import fr.afcepf.ai85.ent.Facture;
import fr.afcepf.ai85.ent.PeriodeInscription;
import fr.afcepf.ai85.hibernate.util.HibernateUtil;

public class ServiceFactureFamille {

	
public List<Facture> rechercherFactureEleveByIdEleve(int idCherche) {
		
		Session session = HibernateUtil.currentSession();
		Transaction tx = session.beginTransaction();

		Query hql3 = session.createQuery(
				"FROM Facture fac WHERE fac.inscriptionPeriscolaire.eleve.idEleve like :idel " );
		hql3.setParameter("idel", idCherche);
		List<Facture> facs = hql3.list();
		
//		Query hql3 = session.createQuery(
//		"FROM Facture fac WHERE fac.idFacture like :idel " );
//hql3.setInteger("idel", idCherche);
//List<Facture> facs = hql3.list();
//		

		for (Facture fc : facs){
//	System.out.println("montant facture :  " + fc.getMontantGobal());
		}
		
		

		HibernateUtil.closeSession();

		return facs;
	}

public List<Facture> listerFactureNonPayees(List<Facture>facs) {
	List<Facture> listfacsNP = new ArrayList<Facture>();
	for (Facture fac : facs){
		if(fac.getDateReglement()==null){
			listfacsNP.add(fac);
		}
	}
	return listfacsNP;	
}

public List<Facture> listerFacturePayees(List<Facture>facs) {
	List<Facture> listfacsP = new ArrayList<Facture>();
	for (Facture fac : facs){
		
		if((fac.getDateReglement()) != null){
			listfacsP.add(fac);
		}
	}
	return listfacsP;	
}

public int compterNbAccueilMatin(int idFacture){
	Session session = HibernateUtil.currentSession();
	Transaction tx = session.beginTransaction();

	Query sqlFact1 = session
			.createSQLQuery("select count(planning_freq_occas.DATE_INSCRIPTION_IRREGULIER) " +
					"from facture, planning_freq_occas " +
					"where facture.ID_FACTURE=? and facture.ID_INSCRIPTION= planning_freq_occas.ID_INSCRIPTION and planning_freq_occas.ID_ACTIVITE=1;");
	sqlFact1.setInteger(0, idFacture);
	BigInteger result= (BigInteger) sqlFact1.uniqueResult();
	int nbMat = result.intValue();
	System.out.println("nombre de matin : " + nbMat);
	return nbMat;	
}

public int compterNbAccueilMidi(int idFacture){
	Session session = HibernateUtil.currentSession();
	Transaction tx = session.beginTransaction();

	Query sqlFact1 = session
			.createSQLQuery("select count(planning_freq_occas.DATE_INSCRIPTION_IRREGULIER) " +
					"from facture, planning_freq_occas " +
					"where facture.ID_FACTURE=? and facture.ID_INSCRIPTION= planning_freq_occas.ID_INSCRIPTION and planning_freq_occas.ID_ACTIVITE=2;");
	sqlFact1.setInteger(0, idFacture);
	BigInteger result= (BigInteger) sqlFact1.uniqueResult();
	int nbMid = result.intValue();
	System.out.println("nombre de midi : " + nbMid);
	return nbMid;	
}

public int compterNbAccueilSoir(int idFacture){
	Session session = HibernateUtil.currentSession();
	Transaction tx = session.beginTransaction();

	Query sqlFact1 = session
			.createSQLQuery("select count(planning_freq_occas.DATE_INSCRIPTION_IRREGULIER) " +
					"from facture, planning_freq_occas " +
					"where facture.ID_FACTURE=? and facture.ID_INSCRIPTION= planning_freq_occas.ID_INSCRIPTION and planning_freq_occas.ID_ACTIVITE=3;");
	sqlFact1.setInteger(0, idFacture);
	BigInteger result= (BigInteger) sqlFact1.uniqueResult();
	int nbSoir = result.intValue();
	System.out.println("nombre de soir : " + nbSoir);
	return nbSoir;	
}


public PeriodeInscription RecupererPeriodeByIdFacture(int idFacture){
	PeriodeInscription pI = null;
	Session session = HibernateUtil.currentSession();
	Transaction tx = session.beginTransaction();

	Query hql2 = session.createQuery(
			"Select fac.inscriptionPeriscolaire.periodeInscription FROM Facture fac WHERE fac.idFacture like :idfac " );
	hql2.setParameter("idfac", idFacture);
	List<PeriodeInscription> ListepIs = hql2.list();
	
	pI = ListepIs.get(0);

System.out.println("date debut :  " + pI.getDateDebutInscription());
	
	HibernateUtil.closeSession();

	return pI;
	
	
}

public static void main(String[] args) {
//	List<Facture> listfact =  rechercherFactureEleveByIdEleve(12);
//	List<Facture> listfactNP = listerFactureNonPayees(listfact);
//	List<Facture> listfactP = listerFacturePayees(listfact);
//	for (Facture f : listfact) System.out.println("date facture : " + f.getDateFacture());
//	for (Facture f : listfactNP) System.out.println("date facure np : " + f.getDateFacture());
//	for (Facture f : listfactP) System.out.println("date facture p : " + f.getDateFacture());
//	Integer nbMatin = comIpterNbAccueilMatin(11);
//	PeriodeInscription perIns = RecupererPeriodeByIdFacture(11);
	
}
}
